# -*- coding:utf8 -*-
__author__ = 'Forcs'

from django.shortcuts import render_to_response

from wanbatvcom.http.decorators import require_params
from wanbatvcom.http.whttp import send_ok_response_with_data, send_error_response_with_message
from wanplus.models import wangwangba

import datetime, time


@require_params(params=["wx_user_id", "wx_nickname", "portrait_url", "video_name", "stb_user_id"], method='POST')
def sync_wangwangshow(request):

    query = request.POST
    wx_user_id = query["wx_user_id"]
    wx_nickname = query["wx_nickname"]
    portrait_url = query["portrait_url"]
    video_name = query["video_name"]
    stb_user_id = query["stb_user_id"]

    datetime_added = datetime.datetime.now()

    timestamp = time.mktime(datetime_added.timetuple()) / 1000

    import hashlib
    md5 = hashlib.md5()
    md5.update("{}{}{}".format(wx_user_id, video_name, timestamp))

    token = md5.hexdigest()

    try:
        model = wangwangba.WangwangShow()
        model.stb_user_id = stb_user_id
        model.wx_user_id = wx_user_id
        model.wx_nickname = wx_nickname
        model.portrait_url = portrait_url
        model.video_url = video_name
        model.date_added = datetime_added
        model.is_invalid = False
        model.is_release = 0
        model.cover_url = ''
        model.token = token
        model.save()

        return send_ok_response_with_data({
            "insert": True,
            "item_name": video_name
        })
    except Exception, e:
        return send_error_response_with_message(u'insert %s %s fail' % (wx_user_id, video_name))


def index_wangwangshow_manage(request):
    return render_to_response("wanplus/wangwangba/wangwangshow.html", {})


def get_wangwangshow_list(request):

    qs = wangwangba.WangwangShow.objects.order_by("-id")

    result_list = None
    try:
        if qs.exists() and qs.count() > 0:
            result_list = list()
            for model in qs:
                item = dict()
                item["videoUrl"] = model.video_url
                item["token"] = model.token
                item["hasCover"] = model.cover_url is not None or model.cover_url is not ''
                item["wxNickname"] = model.wx_nickname
                item["hasPortrait"] = model.portrait_url is not None or model.portrait_url is not ''
                item["datetime"] = model.date_added.strftime("%Y-%m-%d %H:%M:%S")
                item["releaseState"] = model.is_release
                result_list.append(item)

    except Exception as e:
        return send_error_response_with_message(u'后台数据异常')

    if result_list:
        return send_ok_response_with_data({
            "list": result_list
        })
    else:
        return send_error_response_with_message(u'数据为空')


def dogtv_load_covers(request):
    from wanplus.wangwangba.models import WangwangbaDogtvCovers
    try:
        qs = WangwangbaDogtvCovers.objects.all()
        covers_list = list()
        for item in qs:
            covers_list.append({
                "position": item.position,
                "filename": item.filename
            })

        return send_ok_response_with_data(data={
            "list": covers_list
        })

    except Exception as e:
        return send_error_response_with_message(message=e.message)

